package it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents;

import java.util.HashMap;
import java.util.LinkedList;

import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.events.GenericEvent;

public class GraphicEventManager {

	private static GraphicEventManager instance;
	private HashMap<Class, LinkedList<GraphicEventListener>> listeners;
	
	private GraphicEventManager() {
		listeners = new HashMap<>();
	}

	public static GraphicEventManager getInstance() {
		if(instance == null)
			instance = new GraphicEventManager();
		
		return instance;
	}

	public boolean addListener(GraphicEventListener listener, Class event) {
		
		if(!listeners.containsKey(event)){
			
			LinkedList<GraphicEventListener> eventListeners = new LinkedList<>();
			eventListeners.add(listener);
			listeners.put(event, eventListeners);
			return true;
		}
		else
		{
			return listeners.get(event).add(listener);
		}
	}

	public boolean removeListener(GraphicEventListener listener, GenericEvent event) {
		// TODO Auto-generated method stub
		return false;
	}



	public void fireEvent(GraphicEvent event) {
		
				LinkedList<GraphicEventListener> eventListeners = listeners.get(event.getClass());
				
				// if the list of listeners for that particular event is not empty (null)
				if(eventListeners != null)
					for (GraphicEventListener eventListener : eventListeners) 
						eventListener.notify(event);
					
	}


}
